package com.java.module.chat.controller.admin;

import java.util.List;

import com.java.module.chat.domain.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.common.lang.annotation.Log;
import com.java.common.lang.domain.AjaxResult;
import com.java.common.lang.domain.page.TableDataInfo;
import com.java.common.lang.enums.BusinessTypeEnum;
import com.java.common.utils.SecurityUtils;
import com.java.common.utils.WebKit;
import com.java.common.utils.poi.ExcelUtil;
import com.java.module.chat.domain.dto.SessionDTO;
import com.java.module.chat.service.ISessionService;

/**
 * 聊天：会话列Controller
 * 
 * @author Administrator
 * @date 2021-05-13
 */
@RestController("AdminSessionController")
@RequestMapping("/chat/session")
public class SessionController {

    @Autowired
    private ISessionService sessionService;

    /**
     * 查询聊天：会话列列表
     */
    @PreAuthorize("@ss.hasPermi('chat:session:list')")
    @GetMapping("/list")
    public TableDataInfo list(SessionDTO session) {
        session.setUid(0L);
        session.setSessionType(1);
        WebKit.startPage();
        List<Session> list = sessionService.selectSessionList(session);
        return WebKit.getDataTable(list);
    }

    /**
     * 导出聊天：会话列列表
     */
    @PreAuthorize("@ss.hasPermi('chat:session:export')")
    @Log(title = "聊天：会话列", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SessionDTO session) {
        List<Session> list = sessionService.selectSessionList(session);
        ExcelUtil<Session> util = new ExcelUtil<Session>(Session.class);
        return util.exportExcel(list, "session");
    }

    /**
     * 获取聊天：会话列详细信息
     */
    @PreAuthorize("@ss.hasPermi('chat:session:query')")
    @GetMapping(value = "/{sessionId}")
    public AjaxResult getInfo(@PathVariable("sessionId") Long sessionId) {
        return AjaxResult.success(sessionService.selectSessionById(sessionId));
    }

    /**
     * 新增聊天：会话列
     */
    @PreAuthorize("@ss.hasPermi('chat:session:add')")
    @Log(title = "聊天：会话列", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Session session) {
        return WebKit.toAjax(sessionService.insertSession(session));
    }

    /**
     * 修改聊天：会话列
     */
    @PreAuthorize("@ss.hasPermi('chat:session:edit')")
    @Log(title = "聊天：会话列", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Session session) {
        return WebKit.toAjax(sessionService.updateSession(session));
    }

    /**
     * 删除聊天：会话列
     * 
     * @param sessionIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('chat:session:remove')")
    @Log(title = "聊天：会话列", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{sessionIds}")
    public AjaxResult remove(@PathVariable Long[] sessionIds) {
        return WebKit.toAjax(sessionService.deleteSessionByIds(sessionIds));
    }

    /**
     * 获取客服未读消息数
     * 
     * @return
     */
    @GetMapping("/getUnreadMsgCount")
    public AjaxResult getUnreadMsgCount() {
        Session session = new Session();
        session.setUserUnreadCount(
                sessionService.getUnreadMsgCount(SecurityUtils.getLoginUser().getUser().getUserId()));
        return AjaxResult.success(session);
    }

    /**
     * 获取客服未读消息数
     *
     * @return
     */
    @GetMapping("/getUnreadMsgCountAll")
    public AjaxResult getUnreadMsgCountAll() {
        return AjaxResult.success(sessionService.getUnreadMsg(null));
    }

}